<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>w_custom_columns基本信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/qbdmsStatic/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/qbdmsStatic/style/forms/currencyCss.css">
    <link rel="stylesheet" href="${ctx}/qbdmsStatic/style/qbdms.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
    <style>
        .demo-transfer{
            float: left;
            padding: 0 15px;
            -moz-box-sizing: border-box;
            -webkit-box-sizing: border-box;
            -o-box-sizing: border-box;
            -ms-box-sizing: border-box;
            box-sizing: border-box;
            width: 100%;
        }
        .qbdms-label{
            width: 80px;
        }
        .layui-input-block{
            margin-left: 80px;
        }
        .qbdms-oneForm{
            width: 50%;
        }
    </style>
</head>
<body>

<script type="text/html" template>
<form class="layui-form"  requrl="{{ serverPath.qbdmsPath }}/qbdms/wCustomColumns/get" lay-filter="wCustomColumnsDataForm">

    <div class="layui-row" style="padding: 10px">
        <div class="qbdms-oneForm" style="width: 280px;">
            <label class="layui-form-label qbdms-label">名称<span style="color: red;">*</span></label>
            <div class="layui-input-block qbdms-input">
                <input type="text" name="sign"  placeholder="请输入"
                       autocomplete="off" class="layui-input " maxlength="25" fieldname="sign"  lay-verify="required">
            </div>
        </div>
        <div class="qbdms-oneForm" style="width: 280px;margin-left: 49px">
            <label class="layui-form-label qbdms-label">是否默认<span style="color: red;">*</span></label>
            <div class="layui-input-block qbdms-input">
                <select type="text" name="isDefault" class="layui-input filter-value" lay-verify="required">
                    <option value="0">否</option>
                    <option value="1">是</option>
                </select>
            </div>
        </div>
    <div id="transfer" class="demo-transfer"></div>
    </div>
    <div class="layui-form-item" style="position: fixed;bottom: 15px;left: 0;margin-bottom: 0;width: 100%;">
        <div style="text-align: center;">
                <a lay-submit lay-filter="wCustomColumns" class="layui-btn btn-n privateSubmit qbdms-oneBtn">提交</a>
            <button type="reset" id="closeWindow" style="margin-left: 10px;" class="layui-btn btn-n layui-btn-danger btn-save qbdms-oneBtnLine">取消</button>
        </div>
    </div>
</form>
</script>
<script src="${ctx}/qbdmsStatic/public/configFile.js"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    layui.config({
        base: serverPath.qbdmsPath + staticPath.qbdmsPath //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','common','jquery','transfer','form','publicMethods'], function () {
        var $ = layui.jquery;
        var transfer = layui.transfer;
        var common = layui.common;
        var form = layui.form;
        var publicMethods = layui.publicMethods;
        var transferId = $('.layui-form').attr("lay-filter");
        var cols;
        var uri;
        var id = '${id}';
        var isDef;
        //关闭当前页面
        $("#closeWindow").click(function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });
        if(id){
            common.updateForm({
                name:'wCustomColumnsDataForm',
                idValue:"${id}",
                primary:"id",
                getCallback:function(data){
                    if (common.filterRecord(data)) {
                        cols = JSON.parse(data.data.fieldJson);
                        uri = data.data.uri;
                        isDef = data.data.isDefault;
                        renderTransfer();
                        rep();
                    } else {
                        window.parent.layer.alert(data.msg);
                    }
                }
            });
        }else{
            //父页面的父页面 必须要有一个window.tableInstace全局参数
            cols = parent.parent.tableInstace.tRender.config.cols[0];
            uri =parent.parent.location.pathname;
            renderTransfer();
            rep();
        }

        function rep(){
            $.each(cols,function(i,n){
                if (typeof n.templet === 'function') {
                    n.templet = ((n.templet + '').replace(/[\n]/g,""));
                }
            });
            cols = JSON.parse(JSON.stringify(cols));
        }
        function renderTransfer(){
            var allData = [];
            var checkData = [];
            $.each(cols,function(i,n){
                var field = n.field;
                var hide = n.hide;
                if(field && field != 'sn'){
                    allData.push(n);
                    if(!hide || hide == false || hide == 'false'){
                        checkData.push(field)
                    }
                }
            });
            //实例调用
            transfer.render({
                elem: '#transfer'
                ,title: ['不显示字段', '显示字段']
                ,data: allData
                ,parseData: function(allData){
                    return  {
                        "value": allData.field //数据值
                        ,"title": allData.title //数据标题
                        ,"disabled": false  //是否禁用
                        ,"checked": false  //是否选中
                    }
                }
                ,value: checkData
                ,id: transferId //定义唯一索引
                ,height:260
                ,width:250
                ,showSearch: true
            });
        }


        //监听提交
        form.on('submit(wCustomColumns)', function (data) {
            var showData = transfer.getData(transferId); //获取右侧数据
            if(!showData || showData.length === 0){
                layer.alert("保存失败，至少要显示一列！",{title: false,skin: 'confirm-class'});
                return;
            }
            $.each(cols,function(i,n){
                var field = n.field;
                if(!field || field == 'sn'){
                    return true;
                }
                var sign = true;
                $.each(showData,function( ii,nn){
                    if(n.field == nn.value){
                        n.hide = false;
                        sign = false;
                    }
                });
                if(sign){
                    n.hide = true;
                }
                if (typeof n.templet === 'function') {
                    n.templet = ((n.templet + '').replace(/[\n]/g,""));
                }
            });

            data.field.id = id;
            data.field.uri = uri;
            data.field.fieldJson = cols;
            var isDefault = data.field.isDefault;
            $.ajax({
                url: serverPath.qbdmsPath + "/qbdms/wCustomColumns/save",
                type: "POST",
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data.field),
                success: function (data) {
                    if (common.filterRecord(data)) {
                        layer.alert(publicMethods.confirmStyle(data.msg),{title: false,skin: 'confirm-class'}, function (index) {
                            if(isDefault == '1'){
                                parent.parent.tableInstace(JSON.stringify(cols));
                            }else{
                                if(isDef == '1'){
                                    var initCols = parent.parent.tableInstace.initCols;
                                    if(initCols){
                                        $.each(initCols,function(i,n){
                                            if (typeof n.templet === 'function') {
                                                n.templet = ((n.templet + '').replace(/[\n]/g,""));
                                            }
                                        });
                                        parent.parent.tableInstace(JSON.stringify(initCols));
                                    }
                                }
                            }
                            layer.close(index);
                            parent.layui.table.reload('wCustomColumnsTable');
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else {
                        layer.alert(publicMethods.confirmStyle(data.msg),{title: false,skin: 'confirm-class'});
                    }
                }
            });
        });
    });
</script>
</body>
</html>